package com.atguigu.gmall.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @RequestMapping("/list.html")
    public String doSearch(SearchParam searchParam, Model model) {
        Result<Map> result = listFeignClient.doSearch(searchParam);
        model.addAllAttributes(result.getData());
        String urlAndParam = buildUrlAndParam(searchParam);
        String tmParam = buildTmParam(searchParam);
        Map<String, String> orderMap = buildOrderMap(searchParam);
        List<Map<String, String>> mapList = buildattrList(searchParam);
        model.addAttribute("urlParam", urlAndParam);
        model.addAttribute("orderMap", orderMap);
        model.addAttribute("trademarkParam", tmParam);
        model.addAttribute("propsParmList", mapList);
        return "list/index.html";
    }

    private String buildUrlAndParam(SearchParam searchParam) {
        StringBuilder sb = new StringBuilder("list.html?");
        Long category1Id = searchParam.getCategory1Id();
        if (category1Id!= null) {
            sb.append("category1Id=").append(category1Id);
        }
        Long category2Id = searchParam.getCategory2Id();
        if (category2Id != null) {
            sb.append("category2Id=").append(category2Id);
        }
        Long category3Id = searchParam.getCategory3Id();
        if (category3Id != null) {
            sb.append("category3Id=").append(category3Id);
        }
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)) {
            sb.append("keyword=").append(keyword);
        }
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("&trademark=").append(trademark);
        }
        String[] props = searchParam.getProps();
        if (props != null) {
            for (int i = 0; i < props.length; i++) {
                String prop = props[i];
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }

    private Map<String, String> buildOrderMap(SearchParam searchParam) {
        Map<String, String> orderMap = new HashMap<>();

        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                String type = split[0];
                String sort = split[1];
                orderMap.put("type", type);
                orderMap.put("sort", sort);
                return orderMap;
            }
        }
        orderMap.put("type", "1");
        orderMap.put("sort", "asc");
        return orderMap;
    }

    private String buildTmParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if (trademark != null) {
            String[] split = trademark.split(":");
            if (!ArrayUtils.isEmpty(split) && split.length == 2) {
                String tmName = split[1];
                return "品牌" + tmName;
            }
        }
        return null;
    }

    private List<Map<String, String>> buildattrList(SearchParam searchParam) {
        String[] props = searchParam.getProps();
        List<Map<String, String>> list = new ArrayList<>();
        for (int i = 0; !ArrayUtils.isEmpty(props) && i < props.length; i++) {
            String prop = props[i];
            String[] split = prop.split(":");
            if (split != null && split.length == 3) {
                String attrId = split[0];
                String attrValue = split[1];
                String attrName = split[2];
                Map<String, String> propMap = new HashMap<>();
                propMap.put("attrId", attrId);
                propMap.put("attrValue", attrValue);
                propMap.put("attrName", attrName);
                list.add(propMap);
            }
        }
        return list;
    }
}
